<template>
  <el-form
    :model="ruleForm"
    status-icon
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="密码" prop="pass" :rules="rules">
      <el-input v-model="ruleForm.pass"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  name: "FormName",
  data() {
    const validatePass = (rule, value, callback) => {
      if (this.list.length > 0 && value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.pass !== "") {
          this.$refs.ruleForm.validateField("pass");
        }
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
      },
      list: [],
      rules: {
        validator: validatePass,
        trigger: "blur",
      },
    };
  },
  methods: {
    submitForm(formName) {
      setTimeout(() => {
        this.list.push(1);
      }, 1000);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
